package com.junjiao.util.java.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.RequestUtils;

import com.junjiao.util.java.security.Algorithm;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 图片化处理servlet
 * 
 * * * * * * * 备注：注意windows、liunx系统对中文的支持（字体、字库）
 * 
 * @author jiaojun
 * @email eshore_jiaojun@163.com
 * @version 2.0
 * @date 2010-6-12 使用方法： 1.web.xml添加：<br>
 *       <servlet> <servlet-name>imageCreate</servlet-name>
 *       <servlet-class>com.junjiao
 *       .util.java.servlet.ImageCreateServlet</servlet-class>
 *       <load-on-startup>6</load-on-startup> </servlet> <servlet-mapping>
 *       <servlet-name>imageCreate</servlet-name>
 *       <url-pattern>*.imgs</url-pattern> </servlet-mapping> 2.
 */
public class ImageCreateServlet extends HttpServlet {

	private static final Log log = LogFactory.getLog(ImageCreateServlet.class);
	private static final long serialVersionUID = 0xbb41a0911a7bcc31L;
	public static final int DEFAULT_IMAGE_HEIGHT = 12;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		String str = Algorithm.tripleDesDecode(request.getParameter("str"));
		String L = request.getParameter("L");
		String action = RequestUtils
				.getStringParameter(request, "action", null);
		int strLength = str.length();
		int width = 0;
		if ("T".equals(L)) {// 电话
			width = strLength * 8 + 20;
		} else if ("M".equals(L)) {// 地图页地址
			strLength = strLength > 20 ? 20 : strLength;
			width = strLength * 7;
		} else if ("S".equals(L)) {// 结果页地址
			strLength = strLength * 8;
			width = strLength;
		} else if ("o".equals(L)) {
			strLength = new Integer(request.getParameter("strLength"))
					.intValue();
			width = strLength;
		}
		BufferedImage bi = create(request, str, DEFAULT_IMAGE_HEIGHT, width);
		if (action != null) {
			bi = create1(str, DEFAULT_IMAGE_HEIGHT, width);
		}
		encoding(bi, response.getOutputStream());
	}

	private BufferedImage create1(String str, int height, int width) {

		int strLength = str.getBytes().length;
		strLength = strLength > 45 ? 45 : strLength;
		width = strLength * 7;
		BufferedImage bi = new BufferedImage(width, height, 1);
		Graphics2D g = (Graphics2D) bi.createGraphics();
		Font font = new Font("宋体", Font.PLAIN, 12);
		g.setFont(font);
		g.setPaint(new Color(255, 255, 255));
		g.fillRect(0, 0, width, height);
		g.setColor(new Color(0, 0, 0));
		g.drawString(str, 0, 10);

		g.dispose();
		bi.flush();

		return bi;
	}

	private BufferedImage create(HttpServletRequest request, String str,
			int height, int width) {
		int strLength = str.getBytes().length;
		strLength = strLength > 40 ? 40 : strLength;
		width = strLength * 5;
		String L = request.getParameter("L");
		if ("T".equals(L))// 电话
			width = strLength * 7 + 20;
		int length = width;
		BufferedImage bi2 = new BufferedImage(length, height, 1);
		Graphics2D g = bi2.createGraphics();
		Font font = new Font("宋体", Font.PLAIN, 12);
		g.setFont(font);
		g.setBackground(Color.WHITE);
		g.fillRect(0, 0, length, height);
		g.setColor(Color.BLACK);
		g.drawString(str, 0, 10);
		g.dispose();
		bi2.flush();
		return bi2;
	}

	private void encoding(BufferedImage bi, OutputStream out) {
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
		param.setQuality(1.0F, false);
		encoder.setJPEGEncodeParam(param);
		try {
			encoder.encode(bi);
		} catch (IOException ioe) {
			log.error(ioe.getMessage(), ioe);
		}
	}

}